package cn.yedong.user.controller;

import cn.yedong.basic.exception.BusinessException;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.user.service.IUserService;
import cn.yedong.user.domain.User;
import cn.yedong.user.query.UserQuery;
import cn.yedong.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            userService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<User> findAll(){
        return userService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public QueryResult<User> findPage(@RequestBody UserQuery query) {
        return userService.pageQuery(query);
    }

    /**
     * 手机号码注册
     * 使用Map集合接收前端的参数
     * @param map
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody Map<String,String> map){
        try {
            userService.phoneRegister(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，等稍后再试！");
        }
    }

    /**
     * 邮箱注册
     * 使用map集合接收前端参数
     * @param map
     * @return
     */
    @PostMapping("/register/mail")
    public AjaxResult mailRegister(@RequestBody Map<String,String> map){
        try {
            userService.mailRegister(map);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }

    /**
     * 邮箱注册用户-激活
     * @param userId
     * @return
     */
    @GetMapping("/register/mailActive/{userId}")
    public String mailActive(@PathVariable("userId")Long userId){
        try {
            userService.mailActive(userId);
            return "<h1>激活成功<h1><a href='http://127.0.0.1:8081/#/login'>请点击跳转至登陆页面";
        } catch (Exception e) {
            e.printStackTrace();
            return "网络繁忙，请稍后再试！";
        }
    }
}
